package com.example.demo_mysql_mybatisplus.util.easyexcel.exports;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.demo_mysql_mybatisplus.util.easyexcel.converters.BoolEnumConverter;
import com.example.demo_mysql_mybatisplus.util.easyexcel.converters.GenderConverter;
import com.example.demo_mysql_mybatisplus.util.easyexcel.converters.HyperlinkConverter;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.comment.HeadCommentRowWriteHandler;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.dynamic.column.DataSorter;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.selected.ContentSelectedRowWriteHandler;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.strategy.AutoHeadColumnWidthStyleStrategy;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.customized.util.ExportUtil;
import com.example.demo_mysql_mybatisplus.util.easyexcel.exports.dtos.ExportDataDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * EasyExcel 导出 测试
 */
@Controller
@RequestMapping("/easyExcel/demo/export")
public class EasyExcelController {
    /**
    * 使用注解配置导出文件 (按dto(注解)的顺序导出)
    * http://localhost:8080/easyExcel/demo/export/use/annotation
    * */
    @RequestMapping(value = "use/annotation", method = RequestMethod.GET)
    public void exportUseAnnotation(HttpServletResponse response) throws Exception {
        // 配置参数
        String fileName = "useAnnotation";
        Object[] sheet_0 = {0,"sheet_0", ExportDataDTO.class};
        List<ExportDataDTO> dataList = MockDatas.getDdataList();

        // 设置excel下载响应头属性
        ExportUtil.setExcelRespProp(response, ExportUtil.createFileName( fileName ));
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).autoCloseStream(true).build();

        // 设置sheet属性
        WriteSheet writeSheet_0 = EasyExcel.writerSheet( (int)sheet_0[0] , (String)sheet_0[1] )
                .head( (Class) sheet_0[2] ) // DTO类
                .registerWriteHandler(new AutoHeadColumnWidthStyleStrategy()) // 自适应列
                .registerWriteHandler(new HeadCommentRowWriteHandler( (Class) sheet_0[2] )) // 表头批注处理器
                .registerWriteHandler(new ContentSelectedRowWriteHandler( (Class) sheet_0[2] )) // 内容下拉框处理器
                .build();
        // 设置数据并导出
        excelWriter.write( dataList , writeSheet_0);
        excelWriter.finish();
        //excelWriter.close(); // 设置了autoCloseStream就不需要 finish、close
    }

    /**
     * 动态导出列
     * http://localhost:8080/easyExcel/demo/export/customized?isTemplete=true
     * http://localhost:8080/easyExcel/demo/export/customized?isTemplet=
     * */
    @RequestMapping(value = "customized", method = RequestMethod.GET)
    public void exportCustomized(HttpServletResponse response
            ,@RequestParam(value = "isTemplete",required = false) boolean isTemplete) throws Exception {
        // 配置参数
        String fileName = "customized";
        Object[] sheet_0 = {0,"sheet_0", ExportDataDTO.class};
        List<String> fieldsInSpecifiedOrder = MockDatas.fieldsInSpecifiedOrder;
        List<ExportDataDTO> dataList = isTemplete?null:MockDatas.getDdataList();

        // 设置excel下载响应头属性
        ExportUtil.setExcelRespProp(response, ExportUtil.createFileName( fileName ));
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).autoCloseStream(true).build();

        // 处理导出顺序，包括动态表头及其对应的列数据
        DataSorter dataSorter = new DataSorter( fieldsInSpecifiedOrder , dataList , (Class) sheet_0[2] );

        // 设置sheet属性
        WriteSheet writeSheet_0 = EasyExcel.writerSheet( (int)sheet_0[0] , (String)sheet_0[1] )
                .head( dataSorter.getHeads() ) // 自定义的表头集合
                .registerConverter(new LongStringConverter()) // 自定义模式需要将'转换器' 手动 注册进来，否则会失效或者报错
                .registerConverter(new GenderConverter())
                .registerConverter(new BoolEnumConverter())
                .registerConverter(new HyperlinkConverter())
                .registerWriteHandler(new AutoHeadColumnWidthStyleStrategy())
                .registerWriteHandler(new HeadCommentRowWriteHandler( (Class) sheet_0[2] ))
                .registerWriteHandler(new ContentSelectedRowWriteHandler( (Class) sheet_0[2] ))
                .build();
        excelWriter.write( dataSorter.getDatas() , writeSheet_0); // 自定义的列数据
        excelWriter.finish();
    }




}